<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyword" placeholder="关键字"></el-input>
				</el-form-item>
				<el-form-item>
          <!--高级查询需要返回第一页-->
					<el-button type="primary" v-on:click="find">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>


<!--    抽屉-->
    <el-drawer
        title="数据字典明细"
        :visible.sync="table"
        direction="rtl"
        size="50%">
      <el-table :data="details" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column type="index" width="60">
        </el-table-column>
        <el-table-column prop="name" label="明细名" >
        </el-table-column>
        <el-table-column prop="type.name" label="数据字典"  >
        </el-table-column>
        <el-table-column label="操作" width="150px" >
          <template scope="scope">
            <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-drawer>

    <!--
     type="selection"  复选框
     @selection-change  点击复选框的点击事件
   -->
    <el-table :data="types" highlight-current-row v-loading="listLoading" @row-dblclick="show"  @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="name" label="数据字典"  >
      </el-table-column>
      <el-table-column prop="sn" label="编号"  >
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>

    </el-table>


    <!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next"
                     @current-change="handleCurrentChange"
                     :page-size="pageSize"
                     :total="total"
                     :current-page="currentPage"
                     style="float:right;">
			</el-pagination>
		</el-col>
    <!--编辑界面
      :visible.sync  控制dialog是否显示
      :rules   绑定表单校验规则  editFormRules：表单校验规则
      prop="name"  表单属性
      ref 给当前表单取一个引用名字
    -->
		<!--新增或编辑界面-->
		<el-dialog :title="titles" :visible.sync="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="字典名" prop="name">
					<el-input v-model="editForm.name" auto-complete="off"></el-input>
				</el-form-item>
<!--        <el-form-item label="编号" prop="sn">-->
<!--          <el-input v-model="editForm.sn" auto-complete="off"></el-input>-->
<!--        </el-form-item>-->

				<el-form-item label="编号">
          <!--
            <el-option 下拉选项
            :key  选项的唯一标识
            :label 选中之后显示到选择框中的值
            :value 选中之后绑定给模型层的值
              默认value只能绑定值给模型层，如果我们想把一个对象绑定给模型层
              必须要加：value-key="id"
          -->
<!--          <el-select v-model="editForm.type" clearable value-key="id" placeholder="请选择">-->
<!--            <el-option-->
<!--                v-for="item in types"-->
<!--                :key="item.id"-->
<!--                :label="item.name"-->
<!--                :value="item">-->
<!--              <span style="float: left">{{ item.name }}</span>-->
<!--&lt;!&ndash;              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.phone }}</span>&ndash;&gt;-->
<!--            </el-option>-->
<!--          </el-select>-->
          <el-input v-model="editForm.sn" auto-complete="off"></el-input>
				</el-form-item>

			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	export default {
		data() {
			return {
				filters: {
					keyword: '',
				},
        table: false,
        titles:'',
        types: [],
        tyepId:1,
        details:[],
				total: 0,
				// dtotal: 0,
				currentPage: 1,
        pageSize: 5,
				// dcurrentPage: 1,
        // dpageSize: 5,
				listLoading: false,
				sels: [],//列表选中列

				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				editFormRules: {
					name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					],
					sn: [
						{ required: true, message: '请输入编号', trigger: 'blur' }
					]
				},
				//编辑界面数据
				editForm: {
          id: null,
          name: '',
          sn: '',
				},
			}
		},
		methods: {
			handleCurrentChange(val) {
				this.currentPage = val;
				this.getTypes();
			},

      find(){ // 定位到第一页--高级查询必须要返回第一页
			  this.currentPage=1;
			  // 获取数据字典列表
			  this.getTypes();
      },
      show(row){
			  this.tyepId=row.id;
			  this.getDetail();
			  this.table = true;
      },
			//获取数据字典列表
			getTypes() {
				let para = {
					currentPage: this.currentPage, // 动态获取当前页
          pageSize: this.pageSize, // 动态获取当前页数据
          keyword: this.filters.keyword // 动态获取关键字
				};
				this.listLoading = true; // 开启忙加载等待
        this.$http.post("/type",para).then(res =>{ // 通过axios发送异步post请求
          // 成功后
          this.listLoading = false; // 关闭忙等
          this.types = res.data.rows // 获取当前页数据
          this.total = res.data.total; // 获取总数据数
          // 失败后
        }).catch(res=>{

        })

			},
			getDetail() {
				let para = {
					currentPage: this.currentPage, // 动态获取当前页
          pageSize: this.pageSize, // 动态获取当前页数据
          keyword: this.filters.keyword, // 动态获取关键字
          typeId: this.tyepId
				};
				this.listLoading = true; // 开启忙加载等待
        this.$http.post("/detail/"+para.typeId).then(res =>{ // 通过axios发送异步post请求
          // 成功后
          this.listLoading = false; // 关闭忙等
          this.details = res.data //

          this.total = res.data.total; // 获取总数据数
          // 失败后
        }).catch(res=>{

        })

			},


			//删除
			handleDel: function (index, row) { // index下标，row-整行的数据
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
				  // 点了确定之后，发送axios异步请求，url=/type/当前行id（row.id）
          this.$http.delete("/type/"+row.id).then(res=>{
            if(res.data.success){ // 如果成功，执行以下代码
              this.$message({
                message:"操作成功",
                type:"success"
              });
              this.currentPage=1; // 返回到第一页
              this.getTypes(); // 刷新列表
            } else {
              this.$message({
                message:"系统繁忙，稍后重试",
                type:"error"
              });
              this.currentPage=1;
              this.getTypes();
            }
          }).catch(res=>{
            this.$message({
              message:"系统繁忙，稍后重试",
              type:"error"
            })
          })
				});
			},
			//显示编辑界面
			handleEdit: function (index, row) { //
        // this.getDeptTree();
        this.titles="编辑";
				this.editFormVisible = true;
        //数据回显  Object.assign  克隆数据
				this.editForm = Object.assign({}, row);
				// if(row.dirPath){
        //   console.log(row.dirPath)
        //   let arr = row.dirPath.split("/");
        //   let parent=[];
        //   for(let i=1;i<arr.length-1;i++){
        //     parent.push(parseInt(arr[i]));
        //   }
        //   this.editForm.parent = parent;
        // }
			},
			//显示新增界面
			handleAdd: function () {
			  // this.getDeptTree();
			  this.titles="新增";
				this.editFormVisible = true;
				this.editForm = {
          id: null,
          name: '',
          sn: '',
				};
			},
			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true; // 开启按钮忙等
							let para = Object.assign({}, this.editForm);
							// 调整返回给后台的数据格式
              // if(para.parent){ // [2,5]
              //   para.parent = {"id" :para.parent[para.parent.length-1]};
              // }

              this.$http.put("/type",para).then(res=>{
                this.editLoading = false; // 关闭按钮忙等
                this.editFormVisible = false; // 关闭弹出框
                  if(res.data.success){
                    this.$message({
                      message:"操作成功",
                      type:"success"
                    });
                    this.currentPage=1;
                    this.getTypes();
                  } else {
                    this.$message({
                      message:"系统繁忙，稍后重试",
                      type:"error"
                    });
                    this.currentPage=1;
                    this.getTypes();
                  }
                }).catch(res=>{
                  this.$message({
                    message:"系统繁忙，稍后重试",
                    type:"error"
                  })
                })
						});
					}
				});
			},

			selsChange: function (sels) {
				this.sels = sels; // sels是当前选中的所有行的数据
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
          this.$http.patch("/type",ids).then(res=>{
            if(res.data.success){
              this.$message({
                message:"操作成功",
                type:"success"
              });
              this.currentPage=1;
              this.getTypes();
            } else {
              this.$message({
                message:"系统繁忙，稍后重试",
                type:"error"
              });
              this.currentPage=1;
              this.getTypes();
            }
            this.getTypes();
          }).catch(res=>{
            this.$message({
              message:"系统繁忙，稍后重试",
              type:"error"
            })
          })
				});
			},

      // getDetail(row){
			//   this.$http.get("/type",row.id).then(res=>{
			//     this.detailData=res.data;
      //   })
      // }

		},
		mounted() {
			this.getTypes();
		}
	}
</script>

<style scoped>

</style>